package com.zengqingfa.algorithm.sort.bubble;

import static com.zengqingfa.algorithm.PrintUtil.print;
import static com.zengqingfa.algorithm.PrintUtil.swap;

/**
 *
 * @fileName: BubbleSort
 * @author: zengqf3
 * @date: 2021-4-5 21:20
 * @description: 冒泡算法
 */
public class BubbleSort {

    public static final void main(String[] args) {
        int[] arr = new int[]{2, 4, 1, 7, 3, 77, 34};
        print(arr);
        bubbleSort(arr);
        print(arr);
    }

    /**
     * 冒泡排序
     * @param arr
     */
    public static void bubbleSort(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[i] > arr[j]) {
                    swap(arr, i, j);
                }
            }
        }
    }
}
